﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using Register.Command;
using Register.DAL;
using Register.Model;
using System.Web.Script.Serialization;
namespace RegisterWeb.Manager.DoctorPersonalCenter.ashx
{
    /// <summary>
    /// DoctorPersonInfoHandler 的摘要说明
    /// </summary>
    public class DoctorPersonInfoHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
           string action=context.Request["action"];
             string Doctor_ID ="";
           #region 获取医生个人信息
           if (action.Equals("get")) {
              
               List<DoctorUserInfo> list = new List<DoctorUserInfo>();
               list = DoctorPersonInfoBLL.GetDoctorUserInfo("DU20120709002");  //临时模拟赋予一个医生用户ID
               List<DoctorPersonInfoJSON> json = new List<DoctorPersonInfoJSON>();
               foreach (DoctorUserInfo doc in list)
               {
                   List<DoctorInfo> list1 = new List<DoctorInfo>(); //医生用户
                   list1 = DoctorPersonInfoBLL.GetDoctorInfo(doc.Doctor_ID);
                   string DoctorUser_ID = doc.DoctorUser_ID;
                   Doctor_ID = doc.Doctor_ID;
                   string DoctorUser_UserName = doc.DoctorUser_UserName;
                   string DoctorUser_Password = doc.DoctorUser_Password;
                   string DoctorUser_LastRegTime = Convert.ToString(doc.DoctorUser_LastRegTime);
                   foreach(DoctorInfo di in list1){
                      
                       json.Add(new DoctorPersonInfoJSON(DoctorUser_ID, DoctorUserInfoBLL.GetDoctorNameById(Doctor_ID), Doctor_ID,DoctorUser_UserName, DoctorUser_Password, DoctorUser_LastRegTime, DoctorInfoBLL.GetDepartmentsNameById(Convert.ToString(di.Departments_ID)), Convert.ToString(di.Doctor_DateOfBirth).Substring(0,9), HospitalInfoBLL.GetParamNameById(Convert.ToString(di.Doctor_Education)), Convert.ToString(di.Doctor_Email), Convert.ToString(di.Doctor_Gender),Convert.ToString(di.Doctor_Name), Convert.ToString(di.Doctor_No), Convert.ToString(di.Doctor_Phone), Convert.ToString(di.Doctor_Picture),HospitalInfoBLL.GetParamNameById(Convert.ToString(di.Doctor_Position)),di.Doctor_Resume,di.Doctor_Specialty));
                     
                   }
                 
               }

               JavaScriptSerializer jss = new JavaScriptSerializer();
               context.Response.Write(jss.Serialize(json));

           }
           #endregion
           #region 获取评价详情
           else if (action.Equals("getMore")) {
               string evaluationInfo_ID = context.Request["EvaluationInfo_ID"];
               string evaluateContent = DoctorPersonInfoBLL.GetEvaluateContentById(evaluationInfo_ID);
               context.Response.Write("评价详情:" + evaluateContent);
           }
           #endregion
           #region 更新个人信息
           else if (action.Equals("edit"))
           {
               DoctorUserInfo doctor = new DoctorUserInfo();
               doctor.DoctorUser_ID = context.Request["doctorUserID"];
               doctor.DoctorUser_Password = context.Request["DoctorUser_Password"];
               doctor.DoctorUser_UserName = context.Request["DoctorUser_UserName"];
               doctor.DoctorUser_LastAmendTime = DateTime.Now;
               doctor.DoctorUser_LastAmendPerson = "Doublez";

               DoctorInfo doctorInfo = new DoctorInfo();
               doctorInfo.Doctor_DateOfBirth = Convert.ToDateTime(context.Request["Doctor_DateOfBirth"]);
               doctorInfo.Doctor_Email = context.Request["Doctor_Email"];
               doctorInfo.Doctor_Gender = context.Request["Doctor_Gender"];
               doctorInfo.Doctor_ID = context.Request["doctorID"];
               doctorInfo.Doctor_LastAmendPerson = "DoubleZ";
               doctorInfo.Doctor_LastAmendTime = doctor.DoctorUser_LastAmendTime;
               doctorInfo.Doctor_Phone = context.Request["Doctor_Phone"];
               doctorInfo.Doctor_Resume = context.Request["Doctor_Resume"];
               doctorInfo.Doctor_Specialty = context.Request["Doctor_Specialty"];

               DoctorPersonInfoBLL.UpdateDoctorUserInfo(doctor);
               JavaScriptSerializer jss1 = new JavaScriptSerializer();
               context.Response.Write(jss1.Serialize(new message(DoctorPersonInfoBLL.UpdateDoctorInfo(doctorInfo), "个人信息编辑成功！")));

           }
           #endregion
           #region 获取患者评价信息
           else if (action.Equals("getEvaluation"))
           {
               string where = "  Doctor_ID=\'DO20120709001\'";   //此处数据库中存的应该是医生用户ID，而不是医生ID
               int row = int.Parse(context.Request["rows"].ToString());
               int page = int.Parse(context.Request["page"].ToString());
               List<EvaluationInfo> list = new List<EvaluationInfo>();
               list = DoctorPersonInfoBLL.GetPagedEvaluationInfo(row, page, where);

               List<EvaluationInfoJSON> json1 = new List<EvaluationInfoJSON>();
               foreach (EvaluationInfo eva in list)
               {
                   json1.Add(new EvaluationInfoJSON(DoctorUserInfoBLL.GetDoctorNameById(eva.Doctor_ID), Convert.ToString(eva.EvaluationInfo_AddTime), eva.EvaluationInfo_EvalContent, HospitalInfoBLL.GetParamNameById(eva.EvaluationInfo_EvalWay), eva.EvaluationInfo_ID, eva.EvaluationInfo_Status, DoctorPersonInfoBLL.GetPatientNameById(eva.PatientUserInfo_ID)));
               }

               EvaluationInfoListJSON evaluationInfList = new EvaluationInfoListJSON();
               evaluationInfList.total = DoctorPersonInfoBLL.GetEvaluationInfoCount(null);
               evaluationInfList.rows = json1;

               JavaScriptSerializer jss1 = new JavaScriptSerializer();
               context.Response.Write(jss1.Serialize(evaluationInfList));


           }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class EvaluationInfoListJSON
{

    public int total { get; set; }
    public List<EvaluationInfoJSON> rows { get; set; }

}
class EvaluationInfoJSON
{
    public string EvaluationInfo_ID
    {
        set;
        get;
    }
    public string Doctor_ID
    {
        set;
        get;
    }
 
    public string PatientUserInfo_ID
    {
        set;
        get;
    }
    public string EvaluationInfo_EvalWay
    {
        get;
        set;
    }
    public string EvaluationInfo_EvalContent
    {
        get;
        set;
    }

    public string EvaluationInfo_AddTime
    {
        get;
        set;
    }
  
    public string EvaluationInfo_Status
    {
        get;
        set;
    }

    public EvaluationInfoJSON(string Doctor_ID,string EvaluationInfo_AddTime,string EvaluationInfo_EvalContent,string EvaluationInfo_EvalWay,string EvaluationInfo_ID,string EvaluationInfo_Status,string PatientUserInfo_ID)
    {
        this.Doctor_ID = Doctor_ID;
        this.EvaluationInfo_AddTime = EvaluationInfo_AddTime;
        this.EvaluationInfo_EvalContent = EvaluationInfo_EvalContent;
        this.EvaluationInfo_EvalWay = EvaluationInfo_EvalWay;
        this.EvaluationInfo_ID = EvaluationInfo_ID;
        this.EvaluationInfo_Status = EvaluationInfo_Status;
        this.PatientUserInfo_ID = PatientUserInfo_ID;

    }

}
class DoctorPersonInfoJSON
{
    public string DoctorUser_ID
    {
        set;
        get;
    }
    public string Doctor_ID
    {
        set;
        get;
    }
    public string Doctor_IDValue
    {
        set;
        get;
    }
    public string DoctorUser_UserName
    {
        get;
        set;
    }
    public string DoctorUser_Password
    {
        get;
        set;
    }

    public string DoctorUser_LastRegTime
    {
        get;
        set;
    }

    public string Departments_ID
    {
        get;
        set;
    }
    public string Doctor_DateOfBirth
    {
        get;
        set;
    }

    public string Doctor_Education
    {
        get;
        set;
    }
    public string Doctor_Email
    {
        get;
        set;
    }
    public string Doctor_Gender
    {
        get;
        set;
    }

    public string Doctor_Name
    {
        get;
        set;
    }

    public string Doctor_No
    {
        get;
        set;
    }

    public string Doctor_Phone
    {
        get;
        set;
    }

    public string Doctor_Picture
    {
        get;
        set;
    }

    public string Doctor_Position
    {
        get;
        set;
    }




    public string Doctor_Resume
    {
        get;
        set;
    }

    public string Doctor_Specialty
    {
        get;
        set;
    }




    public DoctorPersonInfoJSON(string DoctorUser_ID, string Doctor_ID, string Doctor_IDValue,string DoctorUser_UserName, string DoctorUser_Password, string DoctorUser_LastRegTime, string Departments_ID, string Doctor_DateOfBirth, string Doctor_Education, string Doctor_Email, string Doctor_Gender, string Doctor_Name, string Doctor_No, string Doctor_Phone, string Doctor_Picture, string Doctor_Position, string Doctor_Resume, string Doctor_Specialty)
    {
        this.Departments_ID = Departments_ID;
        this.Doctor_DateOfBirth = Doctor_DateOfBirth;
        this.Doctor_Education = Doctor_Education;
        this.Doctor_Email = Doctor_Email;
        this.Doctor_Gender = Doctor_Gender;
        this.Doctor_ID = Doctor_ID;
        this.Doctor_IDValue = Doctor_IDValue;
        this.Doctor_Name = Doctor_Name;
        this.Doctor_No = Doctor_No;
        this.Doctor_Phone = Doctor_Phone;
        this.Doctor_Picture = Doctor_Picture;
        this.Doctor_Position = Doctor_Position;
        this.Doctor_Resume = Doctor_Resume;
        this.Doctor_Specialty = Doctor_Specialty;
        this.DoctorUser_ID = DoctorUser_ID;
        this.DoctorUser_LastRegTime = DoctorUser_LastRegTime;
        this.DoctorUser_Password = DoctorUser_Password;
        this.DoctorUser_UserName = DoctorUser_UserName;
        


    }

}

